package com.fourth.controller.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fourth.dao.StockDao;
import com.fourth.dao.UBbusinessDao;
import com.fourth.dao.UBcommodityManagerDao;
import com.fourth.service.UBcommodityManagerService;

import com.fourth.vo.CommodityManageVo;
import com.ljx.entity.*;

import com.ljx.security.util.WebUtils;
import com.ljx.utils.JwtUtil;
import com.ljx.utils.OssUtil1;
import com.ljx.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author LJX
 * @Description: TODO
 * @Date: 2023/3/4 15:37
 * @Version 1.0
 */
@RestController
@RequestMapping("/system/commodity")
public class UBcommodityManagerController {
	@Autowired(required = false)
	private UBcommodityManagerService uBcommodityManagerService;
	@Autowired(required = false)
	private UBcommodityManagerDao uBcommodityManagerDao;

	@Autowired(required = false)
	private UBbusinessDao uBbusinessDao;

	@Autowired(required = false)
	private StockDao stockDao;


	/**
	 * 商品管理以及查询数据
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/selectAllGoodsInfoByBusinessId")
	public Result selectAllGoodsInfoByBusinessId(@RequestParam(defaultValue = "1")Integer currentPage,@RequestParam(defaultValue = "5") Integer pageSize,@RequestBody Goods goods){
		return uBcommodityManagerService.selectAllGoodsInfoByBusinessId(currentPage,pageSize,goods);
	}

	/**
	 * 通过id删除商品
	 * @param id
	 * @return
	 */
	@RequestMapping("/goods/deleteGoodsById")
	public Result deleteGoodsById(Integer id){

		int i = uBcommodityManagerDao.deleteGoodsById(id);
		if (i>0){
			return new Result(2000,"删除成功");
		}
		return new Result(2000,"删除失败");
	}

	/**
	 * 修改上下架状态
	 * @param goods
	 * @return
	 */
	@RequestMapping("/goods/updateStatus")
	public Result updateStatus(@RequestBody Goods goods){
		uBcommodityManagerDao.updateStatus(goods);
		return new Result(2000,"修改成功");
	}

	/**
	 * 查商品询储存方式
	 * @return
	 */
	@RequestMapping("/deposit/findAllDeposit")
	public Result findAllDeposit(){
		List<Deposit> allDeposit = uBcommodityManagerDao.findAllDeposit();
		if(allDeposit!=null){
			return new Result(2000,"查询deposit成功",allDeposit);
		}
		return new Result(2000,"查询deposit失败");	}
	/**
	 * 查询商品牌
	 * @return
	 */
	@RequestMapping("/brand/selectAllBrand")
	public Result selectAllBrand(){
		List<Brand> allBrand = uBcommodityManagerDao.selectAllBrand();
		if(allBrand!=null){
			return new Result(2000,"查询brands成功",allBrand);
		}
		return new Result(2000,"查询brands失败");
	}
	/**
	 * 查询商品单位
	 * @return
	 */
	@RequestMapping("/unit/selectAllUnit")
	public Result selectAllUnit(){
		List<Unit> units = uBcommodityManagerDao.selectAllUnit();
		if(units!=null){
			return new Result(2000,"查询units成功",units);
		}
		return new Result(2000,"查询units失败");
	}
	/**
	 * 查询商品类型
	 * @return
	 */
	@RequestMapping("/goods/getAllSort")
	public Result getAllSort(){
		List<Sort> sorts = uBcommodityManagerDao.selectAllSort();
		if(sorts!=null){
			return new Result(2000,"查询sorts成功",sorts);
		}
		return new Result(2000,"查询sorts失败");
	}
	/**
	 * 添加头像上传
	 */

	@RequestMapping("/upload01")
	public Result upload01(MultipartFile file) throws Exception {
		String path = OssUtil1.ossUpload(file);
		return new Result(2000,"上传成功",path);
	}

	/**
	 * 添加商品信息
	 * @param goods
	 * @return
	 */

	@RequestMapping("/goods/addGoods")
	public Result addGoods(@RequestBody CommodityManageVo goods) {

		HttpServletRequest request = WebUtils.getRequest();
		String token = request.getHeader("token");
		Map<String, Object> map = JwtUtil.getTokenChaim(token);
		String username = (String) map.get("username");
		BusinessInfo businessInfo = uBbusinessDao.selectBusinessBytelephone(username);

		goods.setBid(businessInfo.getId());
		goods.setState(2);
		goods.setStatus(1);
		int num=1+(int)(Math.random()*(1000000000-1+1));
		goods.setId(num);
		int update = uBcommodityManagerDao.insert(goods);
		if(update>0){
			// 添加商品信息后 也要把库存信息添进去
			Stock stock = new Stock();
			stock.setGoodsId(goods.getId());
			stock.setTotalNum(goods.getTotalNum());
			String residue = String.valueOf(goods.getTotalNum());
			stock.setResidue(residue);
			int insert = stockDao.insert(stock);
			System.out.println(insert);


			return new Result(2000,"添加成功",update);
		}
		return new Result(2000,"添加失败");
	}

	/**
	 * 修改数据
	 * @param goods
	 * @return
	 */
	@RequestMapping("goods/updateGoods")
	public Result updateGoods(Integer totalNum,@RequestBody Goods goods){
		System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~"+goods);
		int i = uBcommodityManagerDao.updateById(goods);
		if(i>0){
			stockDao.updateStock(goods.getId(),totalNum);
			return new Result(2000,"修改成功",i);
		}
		return new Result(2000,"修改失败",i);
	}
}
